/**
  ******************************************************************************
  * @file: motor_init.c
  * @brief: 初始化定义
  * @author: Mond
  ******************************************************************************
  * @version: 0.1.0
  * @date: 2021/12/9
  * @lastChange: 2021/12/9
  * @history:
  ******************************************************************************
    */

#include "motor_init.h"
#include <stdlib.h>

MotorP motor_1;

/*!
 * 初始化变量
 */
void motor_Init() {
    uint32_t sys_clock = HAL_RCC_GetSysClockFreq();

    /** PID 初始化 **/
    PID_TypeDef *motor_velocity_PID = (PID_TypeDef *) malloc(sizeof(PID_TypeDef));
    motor_velocity_PID->P = 5.5;
    motor_velocity_PID->I = 80;
    motor_velocity_PID->D = 0.01;
    motor_velocity_PID->current = 0;
    motor_velocity_PID->last_bias = 0;
    motor_velocity_PID->integral = 0;
    motor_velocity_PID->result = 0;
    motor_velocity_PID->target = 0;

    /** 电机初始化 **/
    t = (MOTOR_COUNT_HTIM.Instance->PSC + 1.0) * (MOTOR_COUNT_HTIM.Instance->ARR + 1.0) / (sys_clock);
    motor_1 = (MotorP) malloc(sizeof(Motor));
    motor_1->Dir1_GPIOx = MOTOR_DIR1_GPIO_Port;
    motor_1->Dir2_GPIOx = MOTOR_DIR2_GPIO_Port;
    motor_1->Dir1_GPIO_Pin = MOTOR_DIR1_Pin;
    motor_1->Dir2_GPIO_Pin = MOTOR_DIR2_Pin;
    motor_1->Pwm_Htim = &htim3;
    motor_1->Pwm_Channel = TIM_CHANNEL_4;
    motor_1->Velocity_PID = motor_velocity_PID;
    motor_1->Encoder_Resolution = 360;
}